package com.rongshu.view.userInfo.presenter;

import android.content.Context;

import com.rongshu.api.RetorfitService;
import com.rongshu.utils.Common;
import com.rongshu.utils.Constant;
import com.rongshu.utils.UserInfoUtils;
import com.rongshu.view.base.mvp.BasePresenter;
import com.rongshu.view.base.mvp.INetBean;
import com.rongshu.view.userInfo.entity.AtMineMessage;
import com.rongshu.view.userInfo.iview.AtMineView;

import java.util.HashMap;
import java.util.Map;

import rx.Subscriber;

public class AtMinePresenter extends BasePresenter<AtMineView> {

    public AtMinePresenter(Context context) {
        super(context);
    }

    /**
     * @ 我的消息
     */
    public void getAtMyMsg(int user_id, int pageIndex, int pageSize, String sign) {
        RetorfitService.getAtMyMsg(user_id, pageIndex, pageSize, sign)
                .compose(getView().<AtMineMessage>bindToLife())
                .subscribe(new Subscriber<AtMineMessage>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(AtMineMessage atMineMessage) {
                        if (getView() != null) {
                            getView().onAtMyMsg(atMineMessage);
                        }
                    }
                });
    }

    /**
     * 点击@我消息阅读
     * 不绑定生命周期，防止进入下级页面，当前页面的阅读状态无法更新
     */
    public void getSetReadAtMyMsg(int AtMyMsg_id, String sign) {
        RetorfitService.getSetReadAtMyMsg(AtMyMsg_id, sign)
                .subscribe(new Subscriber<INetBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(INetBean bean) {
                        if (getView() != null) {
                            getView().onSetReadeAtMsg(bean);
                        }
                    }
                });
    }

    /**
     * 删除@我的/系统消息
     */
    public void getDeleteMsg(int obj_id, int type, String sign) {
        RetorfitService.getDeleteMsg(obj_id, type, sign)
                .compose(getView().<INetBean>bindToLife())
                .subscribe(new Subscriber<INetBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(INetBean bean) {

                    }
                });
    }

    /**
     * 点击私信/@我的/文章评论消息，标记全部已读
     */
    public void GetMsgAllRead() {
        Map<String, Object> map = new HashMap<>();
        map.put("user_id", UserInfoUtils.getUserId());
        map.put("type", Constant.AT_ME);
        RetorfitService.GetMsgAllRead(UserInfoUtils.getUserId(), Constant.AT_ME, Common.encryptionSign(map))
                .compose(getView().<INetBean>bindToLife())
                .subscribe(new Subscriber<INetBean>() {
                    @Override
                    public void onCompleted() {

                    }

                    @Override
                    public void onError(Throwable e) {

                    }

                    @Override
                    public void onNext(INetBean bean) {

                    }
                });
    }
}
